<?php
// +----------------------------------------------------------------------
// | 车钉钉 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.twothink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: YagamiZN
// +----------------------------------------------------------------------

namespace app\admin\controller;

/**
 * 管理员控制器
 */
class Admin extends Base{
    /**
     * 前置操作方法列表
     * @var array $beforeActionList
     * @access protected
     */
    protected $beforeActionList = ['baseInfo'];
    /**
     * 构造方法，初始化站点信息
     */
    protected function _initialize(){
        parent::_initialize();
    }
    //模版信息初始化
    public function baseInfo(){
        $baseWebmsg=cache('baseWebMsg');
        $this->assign('baseWebMsg',$baseWebmsg);
    }
    
	//管理员列表
    public function index(){
        $adminRole=db('admin_role')->select();
        $this->assign('adminRole',$adminRole);
        $admin=model('Admin');
        $adminListObj=$admin->select();
        $adminList=collection($adminListObj)->toArray();
        $this->assign('adminList',$adminList);
        return $this->fetch();
    }
    //管理员权限
    public function auth(){
        return $this->fetch();
    }
    //管理员权限
    public function adminInfo(){
        return $this->fetch();
    }
    //添加管理员
    public function adminAdd(){
        $name=input('post.name');
        $tel=input('post.tel');
        $pwd=input('post.pwd');
        $repwd=input('post.repwd');
        $role=input('post.role');
        $mark=input('post.mark','','strip_tags,htmlspecialchars');
        if(!preg_match("/^[A-Za-z0-9_]{3,}$/", $name)){
            $this->error("管理名称不能有特殊字符",$_SERVER['HTTP_REFERER'],'',config('jump_time_n'));
            //return ['code'=>300,'msg'=>'管理名称不能有特殊字符'];
        }elseif(empty($tel)||!preg_match(ZZ_PHONE, $tel)){
            $this->error("手机号格式错误",$_SERVER['HTTP_REFERER'],'',config('jump_time_n'));
            //return ['code'=>300,'msg'=>'手机号格式错误'];
        }elseif (empty($pwd)||!preg_match(ZZ_PASSWORD, $pwd)){
            $this->error("密码不合法",$_SERVER['HTTP_REFERER'],'',config('jump_time_n'));
            //return ['code'=>300,'msg'=>'密码不合法'];
        }elseif ($pwd!==$repwd){
            $this->error("两次密码输入不一致",$_SERVER['HTTP_REFERER'],'',config('jump_time_n'));
            //return ['code'=>300,'msg'=>'两次密码输入不一致'];
        }elseif (empty($role)){
            $this->error("请选择角色",$_SERVER['HTTP_REFERER'],'',config('jump_time_n'));
            //return ['code'=>300,'msg'=>'请选择角色'];
        }else {
            $isName=json_decode($this->isAdminName($name),true);
            if($isName['code']!='200'){
                $this->error($isName['msg'],$_SERVER['HTTP_REFERER'],'',config('jump_time_n'));
            }
            $user=model('Admin');
            $pwdkey=mt_rand(0,9999);
            $data=['aname'=>$name,'telephone'=>$tel,'apwd'=>$pwd.$pwdkey,'pwdkey'=>$pwdkey,'role'=>$role,'mark'=>$mark];
            $user->save($data);
            $this->success("添加成功",$_SERVER['HTTP_REFERER'],'',config('jump_time_y'));
            //return ['code'=>200,'msg'=>'添加成功'];
        }
    }
    //检测帐号是否已经被注册ajax
    public function isAdminName($name){
        $isOK=db('admin')->where('aname',$name)->find();
        if(empty($isOK)){
            return json_encode(['code'=>200,'msg'=>'该名称可以注册']);
        }else{
            return json_encode(['code'=>300,'msg'=>'该管理员已经存在']);
        }
    }
}
